package com.example.demo.thread;

/**
 * @Classname PrintThread1
 * @Description
 * @Date 2023/12/12 14:07
 * @Created by jyl
 */
public class PrintThreadBySync1 implements Runnable {
    private static final Object LOCK = new Object();

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            synchronized (LOCK) {
                System.out.println("线程1：" + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static class PrintThreadBySync2 implements Runnable {
        private static final Object LOCK = new Object();

        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                synchronized (LOCK) {
                    System.out.println("线程2：" + i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

        public static void main(String[] args) {
            Thread thread1 = new Thread(new PrintThreadBySync1());
            Thread thread2 = new Thread(new PrintThreadBySync2());
            thread1.start();
            thread2.start();
        }
}




